/*********************************************************************
 * Módulo 3. Tutorial de desarrollo con NME. Mini-Juego: Bee Adventures 
 * Programación Multimedia y Juegos
 * Autor: David Vallejo Fernández    david.vallejo@tegnix.com
 * Autor: Carlos González Morcillo   carlos.gonzalez@tegnix.com
 *
 * You can redistribute and/or modify this file under the terms of the
 * GNU General Public License ad published by the Free Software
 * Foundation, either version 3 of the License, or (at your option)
 * and later version. See <http://www.gnu.org/licenses/>.
 *
 * This file is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.  
 *********************************************************************/

package;

import util.FpsLabel;
import util.ScoreBoard;
import graphics.sprites.SimpleOverlay;
import graphics.ParallaxManager;
import graphics.ActorManager;

import aze.display.TileLayer;
import aze.display.TileSprite;
import aze.display.SparrowTilesheet;
import nme.Assets;
import nme.display.Sprite;
import nme.events.Event;
import nme.events.KeyboardEvent;
import nme.events.MouseEvent;
import nme.Lib;

class BeeGame extends Sprite {
  var _overlay:SimpleOverlay;           // Imagen del marcador
  var _scoreBoard:ScoreBoard;           // Actualiza los puntos
  var _fpsText:FpsLabel;                // Muestra los FPS
  var _parallaxManager:ParallaxManager; // Scroll Manager
  var _actorManager:ActorManager;       // Manager de Actores

  var _layer:TileLayer;                 // Capa Pcpal. de dibujado
  var _previousTime:Int;                // Para calcular Delta(t)

  // Constructor =====================================================
  public function new() {
    super(); haxe.Timer.delay(init, 150);
  }
	
  function init():Void {
    createScene();
    addListeners();
    _previousTime = Lib.getTimer();
  }

  // Carga de Recursos ===============================================
  function loadAssets():Void{
    var sheetData:String = Assets.getText("assets/bee.xml");
    var tilesheet:SparrowTilesheet = new SparrowTilesheet
      (Assets.getBitmapData("assets/bee.png"), sheetData);
    _layer = new TileLayer(tilesheet);
    addChild(_layer.view);
  }

  // Creación de la Escena ===========================================
  function createScene():Void {
    loadAssets();

    _parallaxManager = new ParallaxManager(_layer);
    _parallaxManager.addSky("sky");
    _parallaxManager.addBackgroundStrip("2cloud2", 10, 1.7, 300, -5);
    _parallaxManager.addBackgroundStrip("2cloud1", 10, 2, 320, 10);
    _parallaxManager.addBackgroundStrip("trees", 4, 1, 220, 40);
    _parallaxManager.addBackgroundStrip("sheeps", 4, 1, 160, 140);
    _parallaxManager.addBackgroundStrip("clouds", 4, 1.3, 280, 80);
    _parallaxManager.addBackgroundStrip("flowers", 4, 1, 70, 300);

    _overlay = new SimpleOverlay("scoreboard", 70, 75); 
    _scoreBoard = new ScoreBoard(20,20,0xff0000,"assets/margarosa.ttf");
    _fpsText = new FpsLabel(25, 110, 0x606060, "assets/cafeta.ttf");  
    _layer.addChild(_overlay); addChild(_scoreBoard); addChild(_fpsText);
  
    _actorManager = new ActorManager(_layer, stage.stageWidth, 
				    stage.stageHeight, _scoreBoard);

    _actorManager.addPlayer("bee");
  }    

  // Bucle Principal =================================================
  function mainLoop(eTime:Int):Void {
    _fpsText.update();
    _parallaxManager.update(stage.stageWidth, stage.stageHeight, eTime);
    _actorManager.update(stage.stageWidth, stage.stageHeight, eTime);
    _layer.render();
  }

  // Listeners =======================================================
  function addListeners():Void {
    stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);
    stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPressed);
    stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
  }
  
  function onKeyPressed(event:KeyboardEvent):Void {
    _actorManager.onKeyPressed(event);
  }

  function onMouseMove(event:MouseEvent):Void {
    _actorManager.onMouseMove(event);
  }

  function onEnterFrame(event:Event):Void {
    var now = Lib.getTimer();
    var elapsedTime = now - _previousTime;
    _previousTime = now;
    mainLoop(elapsedTime);
  }

  // Main ============================================================  
  static public function main() {
    Lib.current.addChild(new BeeGame());
  }

}
